I was wondering whether this feature has been built-in via specgram which I believe isn't the case. If not, then the best possible way to implement this using specgram or via matplotlib. If some transformation of the specgram axis can enable this feature.
For example with specgram plots the below won't work(any audio signal can be passed as first argument of specgram):
fig = figure(11)
ax = fig.add_subplot(111)
ax.specgram(tidig.train_symbol_list[0].audio,Fs=12000)
ax.set_yscale('log')
1 Answer 1
You can change the scaling of axes using the Axes.set_xscale
and Axes.set_yscale
functions, which both accept either linear
, log
or symlog
as input. So to change a plot to have a log-scaled x axis you would do something like:
import matplotlib.pyplot as plt
ha = plt.subplot(111)
# Plot your spectrogram here...
ha.set_xscale('log')
Edit This seems to be a known issue. There are the commands available to do this but not in any particularly convenient way (it should just be a flag to the specgram
function or set_xscale
and set_yscale
should work). However, there is a way to do this:
Instead of using matplotlib.pyplot.specgram
use matplotlib.mlab.specgram
. This computes the spectogram but does not draw it. You can then use matplotlib.pyplot.pcolor
or a similar function to plot the spectogram. So try something like:
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
# Set up your data here...
Pxx, freq, t = mlab.specgram(x) # Other options, such as NFFT, may be passed
# to `specgram` here.
ha = plt.subplot(111)
ha.pcolor(t, freq, Pxx)
ha.set_xscale('log')
-
It does not seem so easy... when I test this, it raises an error, whether I call the set_xscale before or after calling specgram.Alexis– Alexis2012年05月30日 08:43:46 +00:00Commented May 30, 2012 at 8:43
-
I tested this on Python 2.6 with matplotlib 1.0.1 and it works (although I just plotted a line rhather than a spectogram). What error do you get. As ever, it is useful to include some code in your question showing exactly what you are trying to achieve.Chris– Chris2012年05月30日 08:55:35 +00:00Commented May 30, 2012 at 8:55
-
Hi Chris, I'm not the one who asked the question, I just wanted to find an answer, so I have downloaded the specgram_demo.py and tried this, and finally got a ValueError. However I'm using Python 2.6.5 with matplotlib 0.99.1.1...Alexis– Alexis2012年05月30日 09:09:57 +00:00Commented May 30, 2012 at 9:09
-
@Chris That doesn't work with specgram plots. I run the current repository at 1.1.0 and python 2.6.5 currently. I know how to change on other plots the axis. Specgram uses an image I believe so can't just change the axis like that. I believe you need some sort of image transformation. I think this capability is available I'm just not sure how to implement it correctly. I added relevant code above so if you can get it working like that then let me know. For the audio just put in any sine wave or import a section of a wav file using scikits.audiolabJ Spen– J Spen2012年05月30日 12:07:37 +00:00Commented May 30, 2012 at 12:07
-
This seems to be a know limitation of matplotlib, it should be easier to do than it is. It can be done however, see my edit.Chris– Chris2012年05月30日 14:59:08 +00:00Commented May 30, 2012 at 14:59